﻿using System;
using System.ServiceProcess;
using Wicresoft.RDW.Installer;

namespace Wicresoft.RDW.Service
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                ProcessCommandLine(args);
            }
            else
            {
                RunService();
            }


        }

        private static void RunService()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new RequestService() 
			};
            ServiceBase.Run(ServicesToRun);
        }

        private static void ProcessCommandLine(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("================================");
                Console.WriteLine("For help, please run : Wicresoft.RDW.Service.exe -help");
                Console.WriteLine("Press Ctrl + C to exit!");
                Console.WriteLine("================================");
                RunInCommandLine();
                return;
            }

            foreach (var x in args)
            {
                switch (x)
                {
                    case "-i":
                    case "-install":
                        InstallService();
                        return;
                    case "-u":
                    case "-uninstall":
                        UninstallService();
                        return;
                    case "-h":
                    case "-help":
                        ShowCommandHelp();
                        return;
                    default:
                        Console.WriteLine("Unknown argument: {0}", x);
                        return;
                }
            }
        }

        private static void ShowCommandHelp()
        {
            Console.WriteLine(@"
================================
Usage: Wicresoft.RDW.Service.exe [-i | -install | -u | -uninstall | -h | -help]

-i
-install
    install windows service
-u
-uninstall
    uninstall windows service
-h
-help
    show usage information
================================
");
        }

        private static void InstallService()
        {
            var installer = new System.Configuration.Install.AssemblyInstaller(typeof(ProjectInstaller).Assembly, null);
            installer.UseNewContext = true;
            installer.Install(null);
        }

        private static void UninstallService()
        {
            var installer = new System.Configuration.Install.AssemblyInstaller(typeof(ProjectInstaller).Assembly, null);
            installer.UseNewContext = true;
            installer.Uninstall(null);
        }


        private static void RunInCommandLine()
        {
            RequestService s = new RequestService();
            s.TaskStart(null);
            Console.ReadLine();
            s.Stop();
        }

    }
}
